package com.ttflutter.tkvideodownloader.pay

import android.text.TextUtils
import com.alibaba.fastjson.JSONException
import com.android.billingclient.api.Purchase
import com.ttflutter.tkvideodownloader.TTT
import org.json.JSONObject

/**
 * @author Afra55
 * @date 2019-07-31
 * A smile is the best business card.
 */
data class GooResultBean(
    val a: String,
    val mSignature: String,
    val productId:String,
    val orderId: String,
    val packageName: String,
    val purchaseTime: Long,
    val purchaseToken: String,
    val purchaseState: Int,
    val developerPayload: String?,
    val acknowledged: Boolean,
    val autoRenewing: Boolean
){

    fun isPurchaseStatePurchased() :Boolean{
        return purchaseState == Purchase.PurchaseState.PURCHASED
    }

    override fun equals(other: Any?): Boolean {
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        // catch TTT
        if (this === other) {
            return true
        }

        if (other !is GooResultBean) {
            return false
        }

        val purchase = other as GooResultBean? ?: return false

        return TextUtils.equals(a, purchase.a) && TextUtils.equals(
            mSignature,
            purchase.mSignature
        )
    }

    override fun hashCode(): Int {
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        // catch TTT
        return a.hashCode()
    }

    fun isPurchaseStatePending() : Boolean{
        return purchaseState == Purchase.PurchaseState.PENDING
    }

    companion object{
        @JvmStatic
        fun cloneByPurchase(purchase: Purchase): GooResultBean {
            return GooResultBean(
                purchase.originalJson,
                purchase.signature,
                purchase.sku,
                purchase.orderId,
                purchase.packageName,
                purchase.purchaseTime,
                purchase.purchaseToken,
                purchase.purchaseState,
                purchase.developerPayload,
                purchase.isAcknowledged,
                purchase.isAutoRenewing
            )
        }
    }
}